<?php

namespace App\Http\Requests\Sku;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;

class ProductStore extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'title' => 'required',
            'parent_category' => 'required',
            'category_id' => 'required',
            'shop_id' => 'required',
            'origin' => 'required|numeric',
            'price' => 'required|numeric',
            'settlement' => 'required|numeric',
            'stock' => 'required',
            'effect' => 'required',
            'date_n' => 'required_if:effect,1',
            'date_begin' => 'required_if:effect,2',
            'date_end' => 'required_if:effect,2',
            //'cover_pic' => 'required|dimensions:ratio=1/1,min_width=100,min_height=100',
            //'share_pic' => 'required|image',
        ];
    }


    public function failedValidation(Validator $validator)
    {
        if ($this->wantsJson() || $this->ajax()) {
            throw new HttpResponseException(
                new JsonResponse([
                    'message' => $validator->errors()->first(),
                    'data' => $validator->errors()
                ],422)
            );
        } else {
            parent::failedValidation($validator);
        }
    }

}
